Skip to content

EmergencyReparentShard: skip zero-position candidates in errant GTID detection - #20831

Merged
timvaillancourt merged 8 commits into
vitessio:mainfrom
timvaillancourt:ers-empty-primary-errant-gtids
Aug 18, 2026
Merged

EmergencyReparentShard: skip zero-position candidates in errant GTID detection#20831
timvaillancourt merged 8 commits into
vitessio:mainfrom
timvaillancourt:ers-empty-primary-errant-gtids

Conversation

@timvaillancourt

@timvaillancourt timvaillancourt commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes a bug introduced in #20780, which moved the zero-position skip in findErrantGTIDs in a way that let a tablet with an empty GTID position remain a promotion candidate. An empty position corroborates nothing, but its presence could flag a healthy replica's entire GTID set as errant; worst-case, ERS promotes the empty tablet and discards the shard's history 😱

The fix skips zero-position candidates up front; a tablet with no GTIDs can't corroborate anyone and shouldn't be promoted over tablets with real history. Review feedback hardened this further: ERS now fails closed when every tablet holding the deepest reparent journal history has an empty position, and an all-empty shard is only treated as new when the topology agrees it was never initialized

This is very unlikely to be hit in practice, as a tablet only reports an empty position with intact data after something abnormal (a RESET MASTER, a restore that died mid-way, etc). Also #20780 has not shipped in a release, so no released version of Vitess is exposed

Related Issue(s)

Follow-up to #20780

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

This PR was written primarily by Claude Code, including this summary

…detection

A demoted primary with an empty GTID position but the max reparent
journal count was kept as a candidate and its empty position added to
the evidence used to check lagged candidates. Empty evidence
corroborates nothing but disables the accept-when-nothing-to-compare
path in FindErrantGTIDs, so a lagged replica's entire GTID set was
flagged errant, potentially leaving the empty primary as the only
candidate left to promote.

Restore the IsZero skip to the top of the maxLenCandidates loop; the
empty-shard initialization case is still covered by the all-zero early
return.

Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>
Copilot AI balanced review requested due to automatic review settings August 13, 2026 12:15
@github-actions github-actions Bot added this to the v25.0.0 milestone Aug 13, 2026
@vitess-bot vitess-bot Bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Aug 13, 2026
@vitess-bot

vitess-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@timvaillancourt timvaillancourt added Type: Bug Component: VTOrc Vitess Orchestrator integration Component: vtctl and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Aug 13, 2026
@timvaillancourt timvaillancourt self-assigned this Aug 13, 2026

This comment was marked as outdated.

@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.64%. Comparing base (70c7a72) to head (5fce21d).
⚠️ Report is 486 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main   #20831       +/-   ##
===========================================
- Coverage   69.67%   68.64%    -1.03%     
===========================================
  Files        1614       14     -1600     
  Lines      216793     2526   -214267     
===========================================
- Hits       151044     1734   -149310     
+ Misses      65749      792    -64957     
Flag Coverage Δ
partial 68.64% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@timvaillancourt
timvaillancourt marked this pull request as ready for review August 13, 2026 12:54

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f226553a56

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread go/vt/vtctl/reparentutil/emergency_reparenter.go Outdated
…ing the evidence tier

A zero-position tablet holding the max reparent journal count previously
left errant GTID detection with no evidence at all: the lagged candidates
were accepted blindly and never reported as starved, so an errant lagged
replica could survive detection and be elected. Dropping nil/zero-position
candidates before the journal counts are gathered lets the remaining
candidates form the evidence tier and corroborate each other

Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>
Copilot AI review requested due to automatic review settings August 13, 2026 14:45

This comment was marked as outdated.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b8a6b51374

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread go/vt/vtctl/reparentutil/emergency_reparenter.go Outdated

@mattlord mattlord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In go/vt/vtctl/reparentutil/emergency_reparenter.go:1339-1347, it seems like the all-zero shortcut still conflates a new shard with one whose GTID state was wiped on every reachable candidate. It returns before reading _vt.reparent_journal, so zero-position tablets with journal lengths 2 and 1 both remain eligible and findMostAdvanced can break the positional tie using promotion rules, potentially selecting the tablet that missed a promotion. A nonzero journal count means this is not the never-initialized case the shortcut is preserving. I think that we should only allow this shortcut when every candidate’s journal count is zero and fail closed otherwise, with a regression test for all-zero positions plus existing or inconsistent journal history. No?

…e max-journal candidates

The reparent journal counts are now gathered for every candidate before
errant GTID detection, including zero-position tablets whose journal rows
survive a GTID wipe. The all-zero shortcut only treats a shard as
uninitialized when every journal is empty too, and when no candidate with
a real GTID position holds the max journal count, ERS fails closed: the
surviving candidates provably missed a promotion whose content can no
longer be proven. A missing reparent journal table on a tablet with no
GTIDs counts as zero entries so a brand-new shard can still be initialized

Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

@mattlord mattlord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In go/vt/vtctl/reparentutil/emergency_reparenter.go:1547-1555, I think the missing-sidecar tolerance still needs to be gated by shardNeverInitialized, not only by the tablet’s empty position. On an initialized shard, a wiped former primary can have both an empty GTID position and no _vt.reparent_journal; treating that unknown journal depth as zero lets a replica with a potentially older visible journal become the maximum evidence tier and be promoted.

…ble on a never-initialized shard

A missing sidecar journal table on an initialized shard hides an unknown
journal depth: converting it to zero entries would let a survivor with a
potentially older visible journal form the evidence tier alone and be
promoted. The tolerance now also requires the topology's never-initialized
signal, so on an initialized shard the gather fails instead

Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>
Copilot AI review requested due to automatic review settings August 14, 2026 11:32
@timvaillancourt

Copy link
Copy Markdown
Contributor Author

@mattlord right again, fixed in d589308: the missing-sidecar tolerance is now gated on the topology's never-initialized signal too. On an initialized shard a missing journal table stays an error, since it hides an unknown journal depth. Added a regression test with a wiped former primary, missing sidecar, and a lower-journal survivor on an initialized shard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d589308eed

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

}
var starved []string
validCandidates, starved, err = erp.findErrantGTIDs(ctx, validCandidates, stoppedReplicationSnapshot.statusMap, tabletMap, opts.WaitReplicasTimeout, failedEvidence)
validCandidates, starved, err = erp.findErrantGTIDs(ctx, validCandidates, stoppedReplicationSnapshot.statusMap, tabletMap, opts.WaitReplicasTimeout, failedEvidence, shardNeverInitialized)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep zero-position failed waiters in journal evidence

In the normal ERS path, this reruns errant-GTID detection only over the post-wait validCandidates; the preceding failedEvidence loop preserves only non-zero GTID positions. If an all-zero-position tablet has _vt.reparent_journal rows but fails the relay-log wait while another zero-position peer succeeds, the failed tablet is deleted before findErrantGTIDs reads journals, so the survivor can be treated as a never-initialized shard and promoted even though reachable journal history proves a prior promotion. The new fail-closed rule needs to read/count zero-position failed waiters too, or fail closed before dropping them.

AGENTS.md reference: go/vt/vtctl/reparentutil/AGENTS.md:L3-L5

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is real but very unlikely, so we're leaving it as-is. For a zero-position tablet to be a failed waiter, every candidate must be zero-position, and the never-initialized shortcut then only fires when the survivors' journals are empty and the shard record has no primary alias or term. Whatever promotion wrote the dropped tablet's journal rows also wrote the shard record, so reaching a promotion here additionally requires the topology to have been wiped or rebuilt. A tablet that fails its relay log wait is also indistinguishable from one that died seconds before ERS started, which would never have been a candidate at all: ERS can only weigh evidence from tablets that respond

On main this scenario promotes unconditionally, since the all-zero shortcut fires before the journals or the topology are consulted, so this PR strictly narrows the hole rather than introducing it

Comment thread changelog/25.0/25.0.0/summary.md Outdated
Comment on lines +294 to +299
#### <a id="ers-wiped-gtid-fail-closed"/>`EmergencyReparentShard` fails closed when GTID state was wiped</a>

`EmergencyReparentShard` now reads the reparent journal of every candidate before errant GTID detection, including candidates with an empty GTID position. A tablet with an empty position cannot corroborate evidence or be promoted over tablets with real history, but its surviving reparent journal rows still prove the shard has promotion history. When every tablet holding the deepest journal history has an empty position, including when every position is empty on a shard whose journal shows history, ERS now fails instead of promoting a candidate that provably missed a promotion. A shard with empty positions and empty journals everywhere, and whose topology has never recorded a primary, is still treated as uninitialized, where any candidate may become the first primary; on such a never-initialized shard, a missing sidecar reparent journal table on a tablet with no GTIDs counts as an empty journal, so ERS can still initialize a brand-new shard; on an initialized shard a missing journal table hides an unknown journal depth and fails the operation.

**Impact**: ERS invocations that previously promoted an empty-position tablet (discarding the shard's history) or elected a survivor missing a promotion now fail with a `FAILED_PRECONDITION` error naming the wiped tablets. Restore the GTID state or data of a wiped tablet before retrying; removing the wiped tablets from the shard instead would discard the missed promotion's transactions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a fix for a change that wasn't released yet? If so, then I don't think we need to mention it in the release notes (or merge it into the release notes of the actual change this is fixing).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arthurschreiber this is a fix for existing ERS logic, but in hindsight this change might be overkill for the changelog. But to play devil's advocate, a sharp-eyed user might notice the difference

I'm leaning towards dropping the note. Thoughts @mattlord?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we should drop it. Remember that this is not a changelog (that is done separately during the release as a list of all PRs) but instead a release summary (highlights). And the summary should not generally have bug fixes but kew new features, breaking changes, etc. So bug fixes like this should not clutter the summary/highlights.

@mattlord mattlord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous missing-sidecar concern is addressed for initialized shards, but I think one contradictory-state edge remains in go/vt/vtctl/reparentutil/emergency_reparenter.go:1547-1559. shardNeverInitialized suppresses ERNoSuchTable for an empty-position tablet even when another reachable candidate has a nonzero GTID position and a positive journal count. That MySQL evidence proves the shard has promotion history despite the empty topology record, so the missing tablet’s journal depth is still unknown. Mapping it to zero can make the visible journal-count-1 replica the maximum evidence tier and promote it even if the missing tablet formerly had deeper history.

I reproduced this on the current HEAD: findErrantGTIDs(..., shardNeverInitialized=true) returns nil and keeps the nonzero replica. I think that we should only tolerate a missing journal when topology says never initialized and allPositionsZero; the existing maxLen check can then reject any visible journal history. A mixed-state regression test should cover this. No?

It's a pretty distant edge case, but should be easy to address too. What do you think?

Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>
…ble when every position is empty

Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>
Copilot AI review requested due to automatic review settings August 14, 2026 19:28
@timvaillancourt

Copy link
Copy Markdown
Contributor Author

@mattlord fixed in 6f9489c0429a34adde116a92196045a01ea01ee2: the missing-journal tolerance is now gated on the topology's never-initialized signal and every position being empty, so a nonzero position anywhere fails the gather instead of letting the visible journal form the evidence tier. Mixed-state regression test added

For context this edge came from the tolerance added in 8af8950 during review, so it never existed on main or in any release

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

// A missing journal table is only tolerated when the topology says never initialized
// and no candidate has any GTIDs; a nonzero position anywhere proves history, so an
// unreadable journal depth must fail the gather
reparentJournalLen, err := erp.gatherReparenJournalInfo(ctx, validCandidates, tabletMap, waitReplicasTimeout, shardNeverInitialized && allPositionsZero)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here, Reparen[t]

}()
length, err = erp.tmc.ReadReparentJournalInfo(groupCtx, tabletMap[alias].Tablet)
if err != nil && tolerateMissingJournal {
// A brand-new shard has no sidecar tables yet: treat a missing journal

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each tablet should have the full set of sidecar tables once it's available. But this check also doesn't hurt anything and is a safeguard against that behavior changing or having a bug etc.

@mattlord mattlord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just the couple of comments inline. The one is just info, the other regarding the missing [t] in the function name we should clean up first.

Thanks, @timvaillancourt ! ❤️

@arthurschreiber arthurschreiber left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once the small nits Matt pointed out are fixed.

Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>
Copilot AI review requested due to automatic review settings August 18, 2026 15:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@timvaillancourt
timvaillancourt merged commit e5255c9 into vitessio:main Aug 18, 2026
173 of 174 checks passed
@timvaillancourt
timvaillancourt deleted the ers-empty-primary-errant-gtids branch August 18, 2026 16:05
ejortegau added a commit to slackhq/vitess that referenced this pull request Aug 21, 2026
A shard whose every reachable candidate reports a zero GTID position (nil or
a typed-but-empty set such as "MySQL56/") has never seen a promotion: it is
being initialized. Such tablets have no _vt sidecar tables yet, so running
errant-GTID detection reads a reparent journal that does not exist and fails
ERS-for-initialization. Skip detection and accept all candidates in that case.

This stands in for the upstream mechanism (allPositionsZero + shardNeverInitialized
inside gatherReparentJournalInfo), which is entangled with an ERS-refactor chain
this fork has deliberately NOT backported: vitessio#20578 (extraEvidence/starved),
vitessio#20780 (explicit split-brain recovery, which introduced the allPositionsZero
early-return), and vitessio#20831/e5255c93b4 (journal-missing tolerance). A TODO marks
this for retirement once the fork upgrades past those PRs.

This is a prerequisite for aligning FindPositionsOfAllCandidates with upstream's
empty-GTID classification (which will classify a fresh shard as GTID-based).

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants